package org.chatboleh;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.apache.log4j.Logger;

//import org.dyno.visual.swing.layouts.Constraints;
//import org.dyno.visual.swing.layouts.GroupLayout;
//import org.dyno.visual.swing.layouts.Leading;

//VS4E -- DO NOT REMOVE THIS LINE!
public class TestFrame01 extends JFrame {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(TestFrame01.class);

	private JButton jButton0;
	private JPanel p1;
	private static int FORM_WIDTH = 400;
	private static int FORM_HEIGHT = 300;

	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

	public TestFrame01() {
		initComponents();
	}

	private void initComponents() {
		// setLayout(new OverlayLayout(this));
		// add(getJButton0(), new Constraints(new Leading(313, 10, 10), new
		// Leading(304, 10, 10)));
		// p1 = new GridPanel();
		p1 = new JPanel();
		final OverlayLayout overlay = new OverlayLayout(p1);
		p1.setLayout(overlay);
		p1.setBackground(Color.white);

		p1.addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseMoved(MouseEvent e) {
				// logger.debug(e.getPoint());
			}
		});

		Container c = getContentPane();
		c.add(p1);
		// add(p1);

		// p1.add(getJButton0());

		final JLabel label = new JLabel("Drag Me");
		label.setHorizontalAlignment((int) LEFT_ALIGNMENT);
		label.addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseDragged(MouseEvent e) {
				// logger.debug("P1 Mouse       position = " +
				// p1.getMousePosition());
				// logger.debug("Label Relative position = " + e.getPoint());
				try {
					label.setAlignmentX(getRelativeLocation(p1
							.getMousePosition().x, p1.getBounds().width - 1));
					label.setAlignmentY(getRelativeLocation(p1
							.getMousePosition().y, p1.getBounds().height - 1));
					refreshLayout();
				} catch (HeadlessException err) {
					logger.error(err);
				} catch (Exception err) {
					logger.error(err);
				}
			}

			@Override
			public void mouseMoved(MouseEvent e) {
				// TODO Auto-generated method stub

			}

		});
		label.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				// float originX = label.getAlignmentX();
				// float originY = label.getAlignmentY();
				// logger.debug("Point = " + e.getPoint());
				// label.setAlignmentX(originX +
				// getRelativeLocation(e.getPoint().x , FORM_WIDTH));
				// label.setAlignmentY(originY +
				// getRelativeLocation(e.getPoint().y , FORM_HEIGHT));
				// refreshLayout();
				// printLabelLocation(label);
			}

		});
		p1.add(label);
		p1.add(getDragableLabel("Drag Me Again"));
		label.setAlignmentX((float) 0.5);
		label.setAlignmentY((float) 0.5);
		// label.setAlignmentY(-30);
		// jButton0.setAlignmentX(30);
		refreshLayout();
		logger.debug(p1.getBounds().height);
		printLabelLocation(label);
		setSize(FORM_WIDTH, FORM_HEIGHT);
	}

	private JLabel getDragableLabel(String text) {
		final JLabel label = new JLabel(text);
		label.setHorizontalAlignment((int) LEFT_ALIGNMENT);
		label.addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseDragged(MouseEvent e) {
				try {
					label.setAlignmentX(getRelativeLocation(p1
							.getMousePosition().x, p1.getBounds().width - 1));
					label.setAlignmentY(getRelativeLocation(p1
							.getMousePosition().y, p1.getBounds().height - 1));
					refreshLayout();
				} catch (HeadlessException err) {
					logger.error(err);
				} catch (Exception err) {
					logger.error(err);
				}
			}

			@Override
			public void mouseMoved(MouseEvent e) {
			}

		});
		return label;

	}

	private float getRelativeLocation(float point, float base) {
		logger.debug(point + " / " + base + " = " + (point / base));
		return point / base;
	}

	private void printLabelLocation(JLabel label) {
		logger.debug(label.getAlignmentX() + " : " + label.getAlignmentY());
	}

	private JLabel getJLabelX() {
		JLabel l = new JLabel();
		// if (jLabel0 == null) {
		// jLabel0 = new JLabel();
		l.setText("jLabel0");
		// }
		return l;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("jButton0");
			jButton0.setLayout(new BorderLayout());
			jButton0.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				TestFrame01 frame = new TestFrame01();
				frame.setDefaultCloseOperation(TestFrame01.EXIT_ON_CLOSE);
				frame.setTitle("TestFrame01");
				// frame.getContentPane().setPreferredSize(frame.getSize());
				// frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jButton0MouseMouseClicked(MouseEvent event) {
		jButton0.setAlignmentY(20);
		((OverlayLayout) p1.getLayout()).invalidateLayout(p1);
		p1.doLayout();
	}

	private void refreshLayout() {
		((OverlayLayout) p1.getLayout()).invalidateLayout(p1);
		p1.doLayout();
	}

}
